package com.bookstore.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bookstore.Domain.Book;
import com.bookstore.Domain.Shopping;
import com.bookstore.Domain.User;
import com.bookstore.mapper.BookMapper;
import com.bookstore.mapper.ShoppingMapper;
import com.bookstore.Service.ShoppingService;
import com.bookstore.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 未来
 */
@Service
public class ShoppingServiceImpl implements ShoppingService {
    private final ShoppingMapper shoppingMapper;

    private final BookMapper bookMapper;
    private final UserMapper userMapper;


    @Autowired
    public ShoppingServiceImpl(ShoppingMapper shoppingMapper,BookMapper bookMapper,UserMapper userMapper) {
        this.shoppingMapper = shoppingMapper;
        this.bookMapper=bookMapper;
        this.userMapper=userMapper;
    }

    @Override
    public List<Shopping> findAll() {
        return shoppingMapper.selectList(null);
    }

    @Override
    public boolean deleteShoppping(int id) {
        return shoppingMapper.deleteById(id)>0;
    }

    @Override
    public boolean insertShoppping(Shopping shopping) {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("id",shopping.getBookId());
        Book book = bookMapper.selectOne(wrapper);
        //根据id获取信息
        shopping.setName(book.getName());
        shopping.setAmount(book.getPrice());
        shopping.setImage(book.getImage());
        shopping.setBookCategory(book.getCategoryId());
        return shoppingMapper.insert(shopping)>0;
    }

    @Override
    public boolean update(Shopping shopping) {
        return shoppingMapper.updateById(shopping)>0;
    }

    @Override
    public boolean insertDate(Shopping shopping) {
        return shoppingMapper.insert(shopping)>0;
    }

    @Override
    public List<Shopping> getAllShops(String userNumber) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_number",userNumber);
        User user = userMapper.selectOne(wrapper);
        QueryWrapper<Shopping> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",user.getId());
        System.out.println(shoppingMapper.selectList(wrapper1));
        return shoppingMapper.selectList(wrapper1);
    }

    @Override
    public boolean deleteArr(int[] arr) {
        List<Integer> idList = Arrays.stream(arr).boxed().collect(Collectors.toList());
        return shoppingMapper.deleteBatchIds(idList)>0;
    }


}
